package cn.wyl.org.service.impl;

import cn.wyl.basic.service.impl.BaseServiceImpl;
import cn.wyl.basic.util.Md5Utils;
import cn.wyl.basic.util.PageList;
import cn.wyl.basic.util.StrUtils;
import cn.wyl.org.domain.Employee;
import cn.wyl.org.domain.Shop;
import cn.wyl.org.mapper.EmployeeMapper;
import cn.wyl.org.mapper.ShopMapper;
import cn.wyl.org.query.EmployeeQuery;
import cn.wyl.org.service.IEmployeeService;
import cn.wyl.user.domain.Logininfo;
import cn.wyl.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;


/**
 * 业务实现类：员工
 */
@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    LogininfoMapper logininfoMapper;
    @Override
    @Transactional
    public void save(Employee e){
        //获取随机盐值
        String salt = StrUtils.getComplexRandomString(32);
        //获取加密之后的密码
        String md5Pwd = Md5Utils.encrypByMd5(salt+e.getPassword());
        //设置盐值密码
        e.setSalt(salt);
        e.setPassword(md5Pwd);
        //由员工转为登录对象 保存登录对象
        Logininfo logininfo = employee2Logininfo(e);
        logininfoMapper.save(logininfo);
        //通过登录对象获取登录主键，设置员工的登录外键，保存员工
        e.setLogininfo_id(logininfo.getId());
        employeeMapper.save(e);
        //同步更新店铺的管理员为该员工
        Long shop_id = e.getShop_id();
        if (shop_id!=null){
            Shop shop = shopMapper.loadById(shop_id);
            shop.setAdmin_id(e.getId());
            shopMapper.update(shop);
        }
        if (e.getRole_id()!=null)
        //保存中间表的信息
        {employeeMapper.saveEmployeeRole(e);}

    }

    @Transactional
    @Override
    public void remove(Long id) {
        Employee e = employeeMapper.loadById(id);
        //删除登录信息表数据
        logininfoMapper.remove(e.getLogininfo_id());
        //将关联的shop表的adminid删除
        Long shop_id = e.getShop_id();
        Shop shop = shopMapper.loadById(shop_id);
        if (shop!=null){

            shop.setAdmin_id(null);
            shopMapper.update(shop);
        }

        //删除t_employee:删除中间表信息：t_employee_role
        employeeMapper.removeEmployeeRoleByEmployeeId(id);
        //删除employee表数据
        employeeMapper.remove(id);

    }

    @Override
    @Transactional
    public void patchDel(List<Long> ids) {
        for (Long id :ids) {
            remove(id);
        }

    }


    @Override
    @Transactional
    public void update(Employee e){

        //1.处理密码和盐值
        //获取随机盐值
        String salt = StrUtils.getComplexRandomString(32);
        //获取加密之后的密码
        String md5Pwd = Md5Utils.encrypByMd5(salt+e.getPassword());
        e.setSalt(salt);
        e.setPassword(md5Pwd);

        //由员工转为登录对象 更新登录对象 需要手动设置logininfo id
        Logininfo logininfo = employee2Logininfo(e);
        Long logininfo_id = employeeMapper.loadById(e.getId()).getLogininfo_id();
        logininfo.setId(logininfo_id);
        logininfoMapper.update(logininfo);

        //更新员工
        employeeMapper.update(e);
        //更新店铺，设置店铺管理员为该员工
        Long shop_id = e.getShop_id();
        if (shop_id!=null){
            Shop shop = shopMapper.loadById(shop_id);
            shop.setAdmin_id(e.getId());
            shopMapper.update(shop);
        }

        //如果该员工的店铺被删除，关联店铺更新删除该员工
        Shop shop = shopMapper.loadByAdminId(e.getId());
        if (shop!=null&&shop_id==null){
            shop.setAdmin_id(null);
            shopMapper.update(shop);
        }


        // 如果员工改了role_id,中间表t_employee_role也要变
        //删除中间表信息
        employeeMapper.removeEmployeeRoleByEmployeeId(e.getId());

        if (e.getRole_id()!=null)
        //保存新的信息
        {employeeMapper.saveEmployeeRole(e);}

    }

    /**
     * 员工转登录记录Logininfo
     * @param employee
     * @return
     */
    public Logininfo employee2Logininfo(Employee employee){
        Logininfo logininfo = new Logininfo();

        BeanUtils.copyProperties(employee,logininfo);
        //当前对象是管理员，非用户
        logininfo.setDisable(employee.getState()==1?true:false);
        logininfo.setType(0);
        return logininfo;
    }


    @Override
    public List<Employee> getEmployeesByShopId(long shopId) {
        return employeeMapper.getEmployeesByShopId(shopId);
    }
}
